package grape.crm.rest.tag.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.crm.rest.tag.form.TagListForm;
import grape.crm.service.tag.query.TagListQuery;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.validation.Valid;
import grape.crm.rest.tag.form.TagCreateForm;
import grape.crm.rest.tag.form.TagUpdateForm;
import grape.crm.rest.tag.form.TagListPageForm;
import grape.crm.rest.tag.vo.TagVo;
import grape.crm.rest.tag.mapper.TagWebMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import grape.common.rest.mvc.BaseController;
import grape.crm.service.tag.po.Tag;
import grape.crm.service.tag.api.ITagService;
import java.util.List;
/**
 * <p>
 * 标签 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-09-14
 */
@RestController
@RequestMapping("/crm/tag")
@Api(tags = "标签相关接口")
public class TagController extends BaseController<TagVo, Tag> {

    @Autowired
    private TagWebMapper currentWebMapper;
    @Autowired
    private ITagService currentService;



     @ApiOperation("添加标签")
     @PreAuthorize("hasAuthority('tag:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public TagVo create(@RequestBody @Valid TagCreateForm cf) {
         Tag po = currentWebMapper.formToPo(cf);
         return super.create(po);
     }

     @ApiOperation("根据ID查询标签")
     @PreAuthorize("hasAuthority('tag:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public TagVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除标签")
     @PreAuthorize("hasAuthority('tag:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新标签")
     @PreAuthorize("hasAuthority('tag:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public TagVo update(@PathVariable String id,@RequestBody @Valid TagUpdateForm uf) {
         Tag po = currentWebMapper.formToPo(uf);
         po.setId(id);
         return super.update(po);
     }

    @ApiOperation("不分页查询标签")
    @PreAuthorize("hasAuthority('tag:single:list')")
    @GetMapping("/list")
    @ResponseStatus(HttpStatus.OK)
    public List<TagVo> list(TagListForm listForm) {
        Tag po = currentWebMapper.formToPo(listForm);
        TagListQuery tagListQuery = new TagListQuery();
        tagListQuery.setPoQuery(po);
        return super.list(tagListQuery);
    }

    @ApiOperation("分页查询标签")
    @PreAuthorize("hasAuthority('tag:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<TagVo> listPage(TagListPageForm listPageForm) {
         Tag po = currentWebMapper.formToPo(listPageForm);
        TagListQuery tagListQuery = new TagListQuery();
        tagListQuery.setPoQuery(po);
        return super.listPage(tagListQuery,listPageForm);
     }

}
